package com.ycw.service.guoguo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycw.bean.Funding;
import com.ycw.bean.Users;
import com.ycw.dao.FundingMapper;
import com.ycw.dao.UsersMapper;
import com.ycw.service.guoguo.SubscriberManagementService;
import com.ycw.util.Msg;
/**
 * 注册用户管理实现
 * @author gcz
 *
 */
@Service
public class SubscriberManagementServiceImpl implements SubscriberManagementService{

	@Autowired
	private UsersMapper usersMapper;
	@Autowired
	private FundingMapper fundingMapper;
	
	@Override
	public Msg getAllUsers(Integer pageNum, Users users) {
		PageHelper.startPage(pageNum, 5);
		PageInfo<Users> pageInfo = new PageInfo<>(usersMapper.select(users));
		
		return Msg.success().add("pageInfo", pageInfo);
	}

	@Override
	public Msg alertUsersById(Users users) {
		
		if(usersMapper.updateByPrimaryKeySelective(users)>0){
			
			return Msg.success();
		}
		return Msg.fail();
	}

	@Override
	public Msg getUsersMoneyById(Users users) {
		Users user = usersMapper.selectByPrimaryKey(users.getUsId());
		Funding funding = new Funding();
		funding.setFdUsId(users.getUsId());
		List<Funding> list = fundingMapper.select(funding);
		double totalMoney = 0l;
		for (Funding f : list) {
			totalMoney+=f.getFdMoney();
		}
		
		return Msg.success().add("usMoney", user.getUsMoney()).add("fundings", list).add("totalMoney", totalMoney);
	}

	@Override
	public Msg geyUsersById(Users users) {
		return Msg.success().add("users", usersMapper.selectByPrimaryKey(users.getUsId()));
	}

}
